package com.yayo.mangement.conf;

import com.yayo.mangement.info.GeneraterInfo;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.util.Properties;

public class VelocityFactory {

    /**
     * 初始化vm
     */
    public static void initVelocityFactory(){
        Properties properties = new Properties();
        try {
            properties.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
            Velocity.init(properties);
        } catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    /**
     * 获取context （生成文件内变量）
     * @param generaterInfo
     * @return
     */
    public static VelocityContext getVelocityContext(GeneraterInfo generaterInfo){
        VelocityContext context = new VelocityContext();
        context.put("comment",generaterInfo.getComment());
        context.put("packageName",PackageFileConfig.PACKAGE_NAME);
        context.put("author",generaterInfo.getCreatedBy());
        context.put("className",generaterInfo.getClassName());
        context.put("classVariable",generaterInfo.getClassVariable());
        context.put("datetime",generaterInfo.DATE_TIME);
        context.put("columns",generaterInfo.getColumns());
        context.put("primaryKey",generaterInfo.getPrimaryKey());
        context.put("tableName",generaterInfo.getTableName());
        return context;
    }
}
